include('shared.lua')
AccessorFunc( ENT, "g_iType", "Type" )

AccessorFunc( ENT, "g_sOwnerName", "OwnerName" )


local OwnerChanged = function(entity, name, oldval, newval) 
    
    if IsValid( newvalue ) && newvalue:IsPlayer() then
        entity:SetOwnerName( newvalue:GetName() )
    end
end


function ENT:Initialize( )
    self:SetType( SharedBox:GetByModel( self:GetModel() ) )
    
    self.MaxHealth = SharedBox:Health( self:GetType() )
    
    self.Color = team.GetColor( self:GetTeam() )
    
    self:FakeInit()
    
    
    self:SetNetworkedVarProxy("owner", OwnerChanged)
    self:SetOwnerName( self:FWOwner():GetName() )
    
end




function ENT:AllowMove( ply )
    return self:FWOwner() == ply && (SharedBox:Health( self:GetType() ) * NicanSettings.PercentOnBuild) == self:FWHealth()
end

// Default Draw Routine..
function ENT:Draw()    
	self:DrawModel()
	
    /*
     //Sorry for the mess, i was having problems with the tracing
    //I will delete this later, not sure yet if it is fixed
    //I AM STILL WAITING FOR TRACEHULL TO GET FIXED!
	local Center = self:LocalToWorld( self:OBBCenter() )
    local Max = self:LocalToWorld( self:OBBMaxs() )
    local Min = self:LocalToWorld( self:OBBMins() )
    
    //local Min, Max = self:WorldSpaceAABB( )
    local Angles = self:GetAngles()
    
    Min = Min - Center
    Max = Max - Center
    
    
    //Rotate the bondries with the ent
    Max:Rotate( Angles ) 
    Min:Rotate( Angles )
    //Center:Rotate( Angles )
    
    render.SetMaterial( Material( "effects/spark" ) )
    
    //Center = self:LocalToWorld( Center )
    Max = Max + Center
    Min = Min + Center
    
    //self:DrawTraceBeam(Min, Max)
    
    //local TracePoints = {
    //    Angles:Up() * (Max.z - 0.1),
    //    Angles:Up() * (Min.z + 0.1),
    //    Angles:Right() * (Max.y - 0.1),
    //    Angles:Right() * (Min.y + 0.1),
    //    Angles:Forward() * (Max.x - 0.1),
    //    Angles:Forward() * (Min.x + 0.1),
    //}
    
    local TracePointsMin = {
        Vector( Min.x, Min.y, Max.z ),
        Vector( Min.x, Max.y, Min.z ),
        Vector( Max.x, Min.y, Min.z ),
    }
    local TracePointsMax = {
        Vector( Max.x, Max.y, Min.z ),
        Vector( Max.x, Min.y, Max.z ),
        Vector( Min.x, Max.y, Max.z ),
    }
    
	for _, v in pairs(TracePointsMin) do 
	   self:DrawTraceBeam(Min, v)
	end
	
	for _, v in pairs(TracePointsMax) do 
	   self:DrawTraceBeam(Max, v)
	end
			
	
	
end

function ENT:DrawTraceBeam(startpos, endpos)

    local fDelta = 0.5 ^ 0.5
    local sinWave = math.sin( fDelta * math.pi )
    
    render.DrawBeam( startpos, 		
					 endpos,
					 2 + sinWave * 16,					
					 1,					
					 0,				
		  		      Color( 0, 255, 0, 255 ) )
		  	*/	      
end


function ENT:FWHealth()
    return self:GetNWInt("HP")
end

function ENT:OnRemove( )
    local ed = EffectData()
		ed:SetEntity( self )
	util.Effect( "entity_remove", ed, true, true )
	
	self:FakeRemove()
end


function ENT:FakeRemove()
	
end